package com.app.mvvmlearnmodule

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.app.common.activity.BaseActivity
import com.app.common.adapter.BaseRecyclerViewAdapter
import com.app.mvvmlearnmodule.databinding.ActivityMvvmLearnBinding
import com.app.mvvmlearnmodule.databinding.ActivityMvvmLearnItemBinding
import com.app.mvvmlearnmodule.entity.Entity02
import com.app.mvvmlearnmodule.model.MVVMLearnModel

@Route(path = "/mvvm_learn_module/mvvm_learn_activity")
class MVVMLearnActivity : BaseActivity() {

    val TAG = MVVMLearnActivity::class.java.simpleName

    var activityMvvmLearnBinding : ActivityMvvmLearnBinding? = null

    var mvvmLearnModel : MVVMLearnModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        activityMvvmLearnBinding = DataBindingUtil.setContentView(this@MVVMLearnActivity, R.layout.activity_mvvm_learn)

        mvvmLearnModel = ViewModelProviders.of(this).get(MVVMLearnModel::class.java)

        activityMvvmLearnBinding?.entity01 = mvvmLearnModel?.mutableEntity01LiveData?.value

        activityMvvmLearnBinding?.lifecycleOwner = this@MVVMLearnActivity

        activityMvvmLearnBinding?.eventListener = EventListener()

        activityMvvmLearnBinding?.activityMvvmLearnList?.layoutManager = LinearLayoutManager(this)


        activityMvvmLearnBinding?.activityMvvmLearnList?.adapter = EntityAdapter(mvvmLearnModel?.mutableEntity02LiveData?.value!!, this)

    }

    inner class EntityAdapter(data: MutableList<Entity02>,context: Context) : BaseRecyclerViewAdapter<Entity02, EntityAdapter.ViewHolder>(data, context) {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val viewDataBinding = DataBindingUtil.inflate<ActivityMvvmLearnItemBinding>(inflater, R.layout.activity_mvvm_learn_item, parent,false)


            return ViewHolder(viewDataBinding)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val entity02 = data[position]

            holder.activityMvvmLearnItemBinding.setVariable(BR.entity02, entity02)

            holder.activityMvvmLearnItemBinding.activityMvvmLearnItemChange.setOnClickListener {

                entity02.name01 = if (entity02.name01.equals("111111")) {
                    "000000"
                } else {
                    "111111"
                }
                Log.d(TAG, "onBindViewHolder -> position -> $position")
            }
        }
        inner class ViewHolder(val activityMvvmLearnItemBinding: ActivityMvvmLearnItemBinding) : RecyclerView.ViewHolder(activityMvvmLearnItemBinding.root)
    }


    inner class EventListener {

        fun btnClickFirst(view: View) {

            mvvmLearnModel?.updateMutableEntity01()
            Log.d(TAG, "entity01 ->${activityMvvmLearnBinding?.entity01?.btnName01}")
        }

    }

}